<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User as userModel;

class user_index extends Controller
{
    public function user_index(Request $request)
    {
        $user = new userModel();
        $search = $request->get('search');
        $usersearch = isset($search)?$search:'';
        $m = 1;
        $page = $request->get('page');
        $page = isset($page)?$page:1;
        $n = ($page-1) * $m;
        $res = $user->where('uname','like','%'.$usersearch.'%')->skip($n)->take($m)->get();
        $count = $user->where('uname','like','%'.$usersearch.'%')->count('uid');
        return view('user_index',['res'=>$res,'count'=>$count,'search'=>$usersearch,'page'=>$page,'m'=>$m]);
    }
    public function user_refer()
    {
        return view('useradd_index');
    }
    public function user_add(Request $request)
    {
        $file = $request->file('uimage');//接收图片
        $demand = array('png','jpg');//后缀要求
        if(in_array($file->getClientOriginalExtension(),$demand)){
            $path = 'img';//文件名
            $dateName = date('Ymd');//时间
            $suffix = $file->getClientOriginalExtension();//获取后缀
            $fileName = md5(time().rand(1,100)).'.'.$suffix;//使用时间加随机数然后加密作为文件名
            $file->move($path,$fileName);
        }
        $post = $request->all();
        $user = new userModel($post);
        $user->getUpwdAttribute($post['upassword']);
        $user->getRepwdAttribute($post['repassword']);
        $user->uimage = $path.'/'.$fileName;
        $user->save();
        return redirect('/user_index');
    }
    public function user_delete(Request $request)
    {
        $uid = $request -> post('uid');
        $user = new userModel();
        $res = $user->where('uid',$uid)->delete();
        if($res){
            return redirect('/user_index');
        }
    }
    public function user_alter(Request $request)
    {
        $uid = $request->post('uid');
        $user = new userModel();
        $rows = $user->where('uid',$uid)->get();
        return view('userupdate_index',['rows'=>$rows]);
    }
    public function user_update(Request $request)
    {
        $res = $request->all();
        $file = $request->file('uimage');
        $user = new userModel();
        if($file==''){
            if(($res['upassword']!=null) and ($res['repassword']!=null)) {
                $rows = $user->where('uid', $res['uid'])->update(['uname' => $res['uname'], 'upassword' => $user->getUpwdAttribute($res['upassword']), 'repassword' => $user->getRepwdAttribute($res['repassword']), 'utel' => $res['utel']]);
            }else{
                $rows = $user->where('uid', $res['uid'])->update(['uname' => $res['uname'], 'utel' => $res['utel']]);
            }
            if($rows){
                return redirect('/user_index');
            }
        }else{
            $path = 'img';//文件名
            $dateName = date('Ymd');//时间
            $suffix = $file->getClientOriginalExtension();//获取后缀
            $fileName = md5(time().rand(1,100)).'.'.$suffix;//使用时间加随机数然后加密作为文件名
            $file->move($path,$fileName);
            if(($res['upassword']!=null) and ($res['repassword']!=null)) {
                $rows = $user->where('uid', $res['uid'])->update(['uname' => $res['uname'], 'uimage' => $path . '/' . $fileName, 'upassword' => $user->getUpwdAttribute($res['upassword']), 'repassword' => $user->getRepwdAttribute($res['repassword']), 'utel' => $res['utel']]);
            }else{
                $rows = $user->where('uid', $res['uid'])->update(['uname' => $res['uname'], 'uimage' => $path . '/' . $fileName, 'utel' => $res['utel']]);
            }
            if($rows){
                return redirect('/user_index');
            }
        }
    }
}
